﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Core;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Xml.Serialization;
using System.Net;
using System.IO;
using System.Xml;

namespace PlugIns
{
    /// <summary>
    /// Плагин он-лайн публикации рейтингов
    /// </summary>
    public class PublishPlugIn : _BasePlugin
    {
        private const string Name = "Публикация рейтинга в интернете";

        private const string Info = "Плагин экспорта данных.\nОтправка отчётов специальному сервису для публикации\nАвтор: Артамонов Ю.";

        #region _BasePlugin Members

        public string PlugInfo
        {
            get { return Info; }
        }

        public string PlugName
        {
            get { return Name; }
        }

        public void MainMethod(SchoolClass Class, Form Owner)
        {
            try
            {
                HostWindow wnd = new HostWindow();
                wnd.HttpAddress.Text = Properties.Settings.Default.Host;
                if (wnd.ShowDialog() == DialogResult.OK)
                {
                    string host = wnd.HttpAddress.Text;
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(host + "/Public");
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Method = "POST";
                    string xml = RetingManagement.SaveDataXML(Class);
                    SoapHexBinary hex = new SoapHexBinary(Encoding.UTF8.GetBytes(xml));
                    string mes = "xml=" + hex.ToString();
                    req.ContentLength = mes.Length;
                    StreamWriter writer = new StreamWriter(req.GetRequestStream());
                    writer.Write(mes);
                    writer.Flush();
                    writer.Close();

                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                    StreamReader reader = new StreamReader(resp.GetResponseStream());

                    string result = reader.ReadToEnd();
                    result = RenderString(result);
                    MessageBox.Show(result);

                    Properties.Settings.Default.Host = host;
                    Properties.Settings.Default.Save();
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Ошибка");
            }
        }

        private string RenderString(string xml)
        {
            string result = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                result = doc.GetElementsByTagName("string").Item(0).InnerText;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка обработки ответа.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return result;
        }

        #endregion
    }
}
